/***************************************************************** 
 * temp_controll.h
 * 
 * Author     : Benedikt Niedermayr <benbrenson@gmx.de>
 *
 * Description:
 * This file stands for the temperature system protocoll layer
 * and controlls the communication between the master(e.g. GNUBLIN) and the
 * temperature system peripherie. Mainly this file serves different structs
 * corresponding to all possible peripherie (sensors, fans, heat sources) connected 
 * the system.
 *
 *
 */
#ifndef __TEMP_CONTROLL_H
#define __TEMP_CONTROLL_H
#include <time.h>


#define BUFF_SIZE 255


char configfile_path[BUFF_SIZE]="/var/www/scripts/terra_control/config/terra.conf";
char logfile_path_base[BUFF_SIZE]="/var/www/scripts/terra_control/log/";
char errlog_path[BUFF_SIZE]="/var/www/scripts/terra_control/log/errlog";
char tmp_file_path[BUFF_SIZE]="/var/www/scripts/terra_control/.tmp";
char string[BUFF_SIZE];
int control_flag = 0;
int init_flag = 0;
int create_flag = 0;
int wake_flag = 0;
int shutdown_flag = 0;
int hflag = 0;

int first_start;

/* Global time struct */
time_t t;
struct tm *tmnow;
unsigned int tm_use;

/**
 * struct fan - represents a number of fans in this system. 
 * @num: Number of the current fan which is choosen. A higher
 *  number means a more effektive fan is working.
 * @location: Corresponds to the position of the current fan.
 * @cur_speed: Shows the current amount of rotations per minute.
 * @max_speed: Indicates the maximum amount of speed.
 * @min_speed: Indicates the minimum speed of the fan.
 * @force_state: Forces the state of the output. State can only be changed if
 *  force_state=0. For force_state=2 security state (critcal Value overstepped).
 * @get_speed: Get the current speed.
 * @set_speed: Set the current speed.
 */
struct fan {

	int num;
	char location;
	int cur_speed;
	int max_speed;
	int min_speed;
	int force_state;
	int		(*get_speed)(int pnum);
	int 	(*set_speed)(int pnum);
};




/**
 * struct ouput - represents an array of external heat sources.
 * @num: Indicates the number of the heat source.
 * @cur_state: Indicates the state of the heat source (1 = on, 0 = off)
 * @force_state: Forces the state of the output. State can only be changed if
 *  force_state=0. For force_state=2 security state (critcal Value overstepped).
 * @type: This explains the type/class of the output.
 * @location: Specifies the location off the heat source.You can also
 *  specify the name of the heat source(e.g. Sun Lamp)
 * @get_state: set state of light. This is only a handler and will
 *  be mapped to a function which makes hardware access.
 * @set_state: same like get_state.
 */
struct output {
	int num;
	int cur_state;
	int force_state;
	int type;
	char location[BUFF_SIZE];
	int		(*get_state)(int pnum, int state);
	int		(*set_state)(int pnum, int state);

};

/***********************************
 * Define output/heat classes
 * The higher a heat class is, the
 * more effect it has to the system.
 */
#define OUT_GROUND		1
#define OUT_HEAT_LOW	2
#define OUT_HEAT_MIDDLE 3
#define OUT_HEAT_HIGH	4
#define OUT_FAN			5


/**
 * struct sensor - represents an array of multiple sensors
 * @num: Represents the number of the current sensor
 *  For average information of all sensors sensor[0] will be used!
 * @location: This is a string which specifies the location of the 
 *  current sensor.It also sets the name of the log path!
 * @log_path: Path for logfile.
 *  It is empty at sensor [0] and should not be defined here
 * @type: This explains the type/class of the sensor.
 * @cur_val: Current Value of Temperature Sensor.
 * @critical_max: Specifies the maximal Temperature for this sensor. The
 *  system has to act if this Temperature is over steped.
 * @critical_min: Specifies the minimal Temperature for this sensro.
 * @crit_reach: This specifies that a critical value for this sensor has reached.
 *  Set this Var will end up with a message to the server (see check_crit).
 *  0 -> None crit val reached
 *  1 -> Crit min reached
 *  2 -> Crit max reached
 * @get_val: Get the value of the sensor.
 */
struct sensor {
	int num;
	char location[BUFF_SIZE];
	char *log_path;
	int type;
	int cur_val;
	int critical_max;
	int critical_min;
	int crit_reach;
	int 	(*get_val)(int pnum);
};

/****************************************************
 * Define Sensor types
 * There are some types of sensors which
 * have different effects to the output
 * For calculating, the average of all values from the
 * same class will be taken 
 */
#define SENS_OUT		1
#define SENS_STD 		2
#define SENS_HEAT		3
#define SENS_COLD		4
#define SENS_HUMIDITY	5
#define SENS_LIGHT		6





/**
 * struct time - Represents the time_table for an object. It's 
 * @start_ground: This stands for on/off turn time of the OUT_GROUND
 *  output.
 * @end_ground: The same like start_ground.
 * @start_main: This stands for the main light or main heat source.
 * @end_main: Off turn time off main output.
 * cur_day: Displays the current day for the time table.
 */
struct time {
	int start_ground;
	int end_ground;
	int start_main;
	int end_main;
	int day;
	
	
};




/**
 * struct terra - represents the object which will be controlled by the system.
 * @num: Indicates the number of the controlled object.
 * @humidity_s: This value specifies the humidity which should be approached.
 * @temp_in_av_s: This is the value which should be approached.
 * @sun_temp: This value shows the value of the sunplace in a terrarium.
 * @sensor_p: Pointer to struct sensor. Later it will be mapped to sensor[0]. 
 * @light_p: Pointer to struct light or other heat source.
 * It makes sence, if you choose a higher value for a heat source which has
 * more effect to the system (main heat source has the highest number)
 * @fan_p: Pointer to struct fan.
 * @time_def_p: Pointer to struct time_def_p. This pointer will point to dynamically created
   in init_time_table(). 
 *
 */
struct terra {

	int num;
	int humidity_s;
	int temp_in_av_s;
	int temp_standby;
	struct sensor *sensor_p;
	struct sensor *sensor_pf;
	struct output *output_p;
	struct output *output_pf;
	struct fan *fan_p;
	struct fan *fan_pf;
	struct tm *time_p;
};








/**
 * ERROR NUMBERS
 * 
 * 
 */
#define __ERRCONN	1		/* Error while getting some value */
#define __ERRBUFF	2		/* Error with variable Buffer */
#define __ERRLOG 	3		/* Error while opening logfile */
#define __ERRLOG_WRITE 4	/* Error while writing logfile */
#define __ERRFUNC	5		/* Error no handler implemented */
#define __ERRREAD	6		/* Error whith time_table file */
#define __ERRENV    7       /* No Env Variable Defined */
#define __ERRSENS   -99		/* Error while getting sensor value */ 




/****************************************/
/* Now define some basic help functions */
/****************************************/
void clearstring(char *s)
{
	int i = 0;
	while(i<BUFF_SIZE)
	{
		s[i]='\0';
		i++;
	}
}

void clearstring_newline(char *s)
{
	int i = 0;
	while(i < BUFF_SIZE)
	{
		if(s[i] == '\n')		
		s[i]='\0';

		i++;
	}
}
 
#endif
